local limeng = fk.CreateSkill{
    name = "ld__limeng",
}

Fk:loadTranslationTable{
    ["ld__limeng"] = "离梦",
    [":ld__limeng"] = "结束阶段，你可以弃置一张非基本牌选择场上两张可以构成一组珠联璧合关系的武将牌，若这两张武将牌：不属于同一名角色，这两名角色依次对对方造成1点伤害。",
    ["ld__limeng_discard"] = "离梦：弃置一张非基本牌发动",
    ["#ld__limeng-choose1"] = "离梦：选择一名角色的武将牌",
    ["#ld__limeng-choose2"] = "离梦：选择另一名角色的武将牌",

    ["$ld__limeng1"] = "得一人知情识趣，何妨同甘共苦。",
    ["$ld__limeng2"] = "临千军而不改其静，御心无波尔。",
}

limeng:addEffect(fk.EventPhaseStart,{
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    if not (target == player and player:hasSkill(limeng.name) and player.phase == Player.Finish) or player:isNude() then return false end
    local room = player.room
    local companion_map = {}
    for _, p in ipairs(room.alive_players) do
      local current_companion = {}
      for _, q in ipairs(room.alive_players) do
        if Fk.generals[p.general]:isCompanionWith(Fk.generals[q.deputyGeneral]) or
          Fk.generals[p.deputyGeneral]:isCompanionWith(Fk.generals[q.general]) or
          Fk.generals[p.general]:isCompanionWith(Fk.generals[q.general]) or
          Fk.generals[p.deputyGeneral]:isCompanionWith(Fk.generals[q.deputyGeneral]) then
            table.insert(current_companion, q)
        end
      end
      if #current_companion > 0 then
        table.insert(companion_map, {p, current_companion})
      end
    end
    if #companion_map > 0 then
      return true
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local card = room:askToDiscard(player,{
        min_num = 1,
        max_num = 1,
        include_equip = true,
        pattern = ".|.|.|.|.|^basic",
        prompt = "ld__limeng_discard",
        skill_name = limeng.name,
        skip = true,
        cancelable = true,
    })
    if #card > 0 then
      event:setCostData(self, {card = card})
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:throwCard(event:getCostData(self).card[1], limeng.name, player, player)
    local companion_map = {}
    for _, p in ipairs(room.alive_players) do
      local current_companion = {}
      for _, q in ipairs(room.alive_players) do
        if Fk.generals[p.general]:isCompanionWith(Fk.generals[q.deputyGeneral]) or
          Fk.generals[p.deputyGeneral]:isCompanionWith(Fk.generals[q.general]) or
          Fk.generals[p.general]:isCompanionWith(Fk.generals[q.general]) or
          Fk.generals[p.deputyGeneral]:isCompanionWith(Fk.generals[q.deputyGeneral]) then
            table.insert(current_companion, q)
        end
      end
      if #current_companion > 0 then
        table.insert(companion_map, {p, current_companion})
      end
    end
      if #companion_map == 0 or player.dead or player:isNude() then return end
      local can_choose_first = {}
      for _, group in ipairs(companion_map) do
         table.insert(can_choose_first, group[1])
      end
      if #can_choose_first > 0 then
      local to1 = room:askToChoosePlayers(player, {
        targets = can_choose_first,
        min_num = 1,
        max_num = 1,
        prompt = "#ld__limeng-choose1",
        skill_name = limeng.name,
        cancelable = true
      })
       if #to1 > 0 then
        local can_choose_second = {}
        for _, group in ipairs(companion_map) do
          if group[1] == to1[1] then
            can_choose_second = group[2]
          end
        end
        local to2 = room:askToChoosePlayers(player, {
          targets = can_choose_second,
          min_num = 1,
          max_num = 1,
          prompt = "#ld__limeng-choose2",
          skill_name = limeng.name,
          cancelable = true
        })
          if #to2 > 0 then
            local targets = {to1[1], to2[1]}
             if targets[1] ~= targets[2] then
             room:sortByAction(targets)
             for _, p in ipairs(targets) do
              if p:isAlive() then
               for _, q in ipairs(targets) do
                 if p ~= q then
                    room:damage{
                      from = p,
                      to = q,
                      damage = 1,
                      skillName = limeng.name
                    }
                  end
                end
              end
            end
          end
        end
      end
    end
  end,
})


return limeng